<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>阿离博客首页</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    <link rel="shortcut icon" type="image/vnd.microsoft.icon" href="https://avatar.moonshot.cn/avatar/cmri3mecp7fdvr21ch3g" />
</head>
<body>
<div class="top">
<h1>阿离博客首页


{% if current_user.is_authenticated %}
    | 欢迎，{{ current_user.username }}!
        <a href="{{ url_for('logout') }}">登出</a> |
        <a href="{{ url_for('dashboard') }}">发帖/删帖</a>
{% else %}
    <a href="{{ url_for('login') }}">登录</a>|
    <a href="{{ url_for('register') }}">注册</a>
{% endif %}
</h1>
{% with messages = get_flashed_messages() %}
    {% if messages %}
        <ul>
            {% for message in messages %}
                <li>{{ message }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endwith %}
</div>>
<div class="content">
{#<h2>帖子列表</h2>#}
<ul>
    <!-- 循环显示当前页的帖子 -->
    {% for post in posts %}
        <li>
           <div class="post-content">
            <h3><a href="{{ url_for('post_detail', post_id=post.id) }}">标题：{{ post.title }}</a></h3>
           内容：{{ post.content }}
           <small>作者: <a href="{{ url_for('user_posts', user_id=post.author.id) }}">{{ post.author.username }}</a></small>
           <small>发帖时间: {{ post.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</small>
                {% if post.attachment %}
                    <br>
                    <strong>附件:</strong>
                     {% set filename = post.attachment %}
<a href="{{ url_for('static', filename='uploads/' + post.id|string + '/' + filename) }}" download style="font-size: 0.85em;">
    {{ filename[:7] + (filename[7:] if '.' not in filename[7:] else '') }}
</a>
                        {% if post.attachment.endswith('.jpg') or post.attachment.endswith('.png') %}
                        <img src="{{ url_for('static', filename='uploads/' + post.id|string + '/thumb_' + post.attachment) }}"
     alt="帖子图片" style="max-width: 50px;" loading="lazy"/>
{#                        {% else %}#}
{#                    <a href="{{ url_for('static', filename='uploads/' + post.id|string + '/' + post.attachment) }}"#}
{#                       download>{{ post.attachment }}</a>#}
                        {% endif %}
            {% endif %}
           </div>
        <div class="post-content">

             <ul style="padding-left: 10px; display: inline-block;">
    <h4 style="margin-bottom: 5px;">最新评论:</h4>
    {% for comment in post.comments[:3] %}
        <li style="margin-bottom: 5px;display: inline-block;">
            <a href="{{ url_for('user_posts', user_id=comment.user.id) }}" style="font-weight: bold; margin-right: 5px;">{{ comment.user.username }}</a>:
            <span style="font-size: 0.9em;">{{ comment.content }}</span>
            <small style="color: #888; font-size: 0.8em;">（{{ comment.created_at.strftime('%Y-%m-%d %H:%M:%S') }}）</small>
        <br>
            {% if comment.attachment %}
                    <strong style="font-size: 0.85em;">评论附件:</strong>
                <br>
                    {% set filename = comment.attachment %}
<a href="{{ url_for('static', filename='uploads/' + post.id|string + '/' + filename) }}" download style="font-size: 0.85em;">
    {{ filename[:7] + (filename[7:] if '.' not in filename[7:] else '') }}
</a>
                {% if comment.attachment.endswith('.jpg') or comment.attachment.endswith('.png') or comment.attachment.endswith('.bmp') %}
                    <img src="{{ url_for('static', filename='uploads/' + post.id|string + '/' + comment.attachment) }}"
     alt="评论附件图片" style="max-width: 40px; max-height: 40px; margin-top: 3px;"/>


                {% endif %}
            {% endif %}
        </li>
    {% endfor %}
    {% if post.comments|length == 0 %}
        <li style="font-size: 0.9em; color: #666;display: inline-block">暂无评论</li>
    {% endif %}
</ul>
        </div>


        <div class="post-actions">
                  <form method="POST" action="{{ url_for('post_detail', post_id=post.id) }}" enctype="multipart/form-data">

                    <textarea name="content" placeholder="请输入评论内容" required></textarea>
                      <br>
                      <input type="file" name="attachment" >
                      <br>
                <button type="submit"style="display: block;text-align: left">发表评论</button>


            </form>
        </div>





        </li>
    {% endfor %}
</ul>
</div>


<div class="footer"><!-- 分页按钮 -->



<!-- 跳转到指定页 -->
<form method="GET" action="{{ url_for('index') }}" class="inline-form">
        {% if page > 1 %}
        <a href="{{ url_for('index', page=page - 1) }}">上一页</a>
    {% endif %}
    <span>第 {{ page }} 页</span>
    {% if has_next %}
        <a href="{{ url_for('index', page=page + 1) }}">下一页</a>
    {% endif %}

    <label for="page_input">跳转到第:</label>
    <input type="number" name="page" id="page_input" min="1" value="{{ page }}">
    <button type="submit">跳转</button> |
</form>

<!-- 控制LED -->
<form id="macForm" onsubmit="return redirectToLED()" class="inline-form">
    <label for="macInput">小工具:</label>
    <input type="text" id="macInput" name="mac" placeholder="输入LED ID" style="width: 30%" required>
    <button type="submit">前往LED控制页面</button>
    |
    <a href="{{ url_for('mqtt')}}" target="_self">MQTT控制页面</a>
    |
    <a href="{{ url_for('files_view')}}" target="_self">网盘</a>
</form>

    </div>

<script>
        function redirectToLED() {
            const macAddress = document.getElementById('macInput').value;
            // 构造目标URL并跳转
            window.location.href = `{{ url_for('led_form', mac='') }}${macAddress}`;
            return false; // 防止表单实际提交
        }
    </script>
</body>
</html>
